﻿using System.Collections;

/// <summary>
/// 主界面流程
/// </summary>
public class HomeProcedure : BaseProceduar
{

	///////////////////////////////////////////////////////////////////////////
	/// <summary>
	/// 主界面UI
	/// </summary>
	UIHomeWindow _ui = null;

	///////////////////////////////////////////////////////////////////////////
	/// <summary>
	/// 单例
	/// </summary>
	static HomeProcedure _inst;

	public static HomeProcedure inst {
		get {
			if (_inst == null) {
				_inst = new HomeProcedure ();
			}
			return _inst;
		}
	}

	///////////////////////////////////////////////////////////////////////////

	public override void EnterProceduar (bool initUI)
	{
		DebugInfo.Log ("进入主界面流程");

		if (initUI) {
			AudioController.inst.PlayBGM ("zhujiemian");

			// 刷新数据
			ReqUserInfo ();
		}
	}

	public override void UpdateProceduar ()
	{
		base.UpdateProceduar ();
	}

	public override void LeaveProceduar ()
	{
		DebugInfo.Log ("退出主界面流程");
	}

	protected override void HandleMessage (Message message)
	{
		switch (message.msgId) {
		case Commands.CMD_MOD_MSG:
			switch (message.iParam) {
			case GameCommands.CMD_JOIN_ROOM:
				GameDataKernel.inst.roomId = (int)message.oParam;
				GameController.inst.SwitchTo (GameProcedure.inst);
				break;

			default:
				break;
			}
			break;

		case Commands.CMD_CTRL_BUTTON_CLICKED:
			switch (message.iParam) {
			case GameCommands.CMD_INVITE_FRIENDS:
				// 邀请好友
				#if UNITY_ANDROID
				WeChat.inst.InviteFriend (false, "悠闲干瞪眼", Constants.SHARE_URL, "快来玩！");
				#endif
				break;
			}
			break;

		default:
			base.HandleMessage (message);
			break;
		}
	}

	protected override bool ProcessPacket (IOMessage message)
	{
		switch (message.code) {
		case IOCmds.SMSG_LOGIN:
			UIRootController.inst.ShowMask (false);

			if (message.state == IOStates.STATE_OK) {
				// 登录成功
				HashtableEx args = message.args;

				UserInfo userInfo = new UserInfo ();
				userInfo.uid = args.GetInt ("uid");

				// INFO
				HashtableEx infoArgs = args.GetMapEx ("info");
				userInfo.name = infoArgs.GetString ("name");
				userInfo.headpic = infoArgs.GetString ("headpic");
				userInfo.sex = infoArgs.GetInt ("sex");
				userInfo.province = infoArgs.GetString ("province");

				// STATUS
				HashtableEx statusArgs = args.GetMapEx ("status");
				userInfo.card = statusArgs.GetInt ("card");
				userInfo.score = statusArgs.GetInt ("score");
				userInfo.playNum = statusArgs.GetInt ("playedNum");

				// MARKS
				HashtableEx marksArgs = args.GetMapEx ("marks");
				userInfo.roomId = marksArgs.GetInt ("ownRoomId");
				if (userInfo.roomId == 0) {
					userInfo.roomId = marksArgs.GetInt ("roomId");
				}

				// 用户信息加载完成
				DataKernel.inst.userInfo = userInfo;

				// 从服务器读取消息
				DataKernel.inst.annInfo.addAnn (args.GetString ("notice"));

				// TODO 从服务器读取公告
				//DataKernel.inst.noticeStr = "我是公告！";

				InitUI ();

				// 登陆成功后初始化语音SDK
				VoiceChat.inst.Init ();

				if (userInfo.roomId != 0) {
					// 加入之前创建的房间
					GameDataKernel.inst.roomId = userInfo.roomId;
					GameController.inst.SwitchTo (GameProcedure.inst);
				}
			}
			return true;

		case IOCmds.SMSG_CREATE_ROOM:
			UIRootController.inst.ShowMask (false);

			if (message.state == IOStates.STATE_OK) {
				GameDataKernel.inst.roomId = message.args.GetInt ("roomId");
				GameController.inst.SwitchTo (GameProcedure.inst);
			}
			return true;

		case IOCmds.SMSG_RESP_GAME_REPORTS:
			UIHome_BattleReport.Popup (message);
			return true;
		}

		return base.ProcessPacket (message);
	}

	/// <summary>
	/// 初始化主界面UI
	/// </summary>
	void InitUI ()
	{
		_ui = new UIHomeWindow ();
		UIRootController.inst.SwitchTo (_ui);

		DebugInfo.Log ("主界面初始化完毕");
	}

	/// <summary>
	/// 发送登录请求
	/// </summary>
	void ReqUserInfo ()
	{
		// 发送登录请求
		UIRootController.inst.ShowMask (true);
		NetIOController.inst.SendMessage (DataKernel.inst.loginReqInfo.ToMessage (IOCmds.CMSG_LOGIN));
	}
}
